#include "StdAfx.h"
#include "IrrVFSystem.h"

#include "Irrlicht.h"
/**
    Get File Extension
*/
#include "./shared/StringUtil.h"
#include "Kernel.h"

extern irr::IrrlichtDevice*  g_IrrDevice;
extern Kernel* g_kernel;





IrrVFSystem::IrrVFSystem(void)
{
    m_IrrFileSys = NULL;
}

IrrVFSystem::~IrrVFSystem(void)
{
}


/** Initialize the file system */
bool IrrVFSystem::Init()
{
    if ( g_IrrDevice )
    {
        m_IrrFileSys = g_IrrDevice->getFileSystem();
        return true;
    } 
    
    g_kernel->LPrintf( "FATAL ERROR:: Could not initialize the Irr File System! -- No Irrlicht Device Found!" );
    return false;
}

/** Shutdown the system */
void IrrVFSystem::Shutdown()
{

}

/** Add a folder to the archive */
bool IrrVFSystem::AddFolder( const std::string &file )
{
    return m_IrrFileSys->addFolderFileArchive( file.c_str() );
}

/** Add a Pak File to the archive */
bool IrrVFSystem::AddPakFile( const std::string &file )
{
    bool success = false;
    if ( StringUtil::ToLower( GetFileExtension( file ) ) == "pak" )
    {
        success = m_IrrFileSys->addPakFileArchive( file.c_str() );
        if ( success )
            g_kernel->LPrintf( ("--Successfully loaded: " + file).c_str() );
        else
            g_kernel->LPrintf( ("**Failed to load: " + file).c_str() );
    } else  g_kernel->LPrintf( ("**WARNING:: " + file + " is not of type PAK!" ).c_str() );
    
    return success;
}

/** Add a Zip File to the archive */
bool IrrVFSystem::AddZipFile( const std::string &file )
{
    bool success = false;
    std::string ext = StringUtil::ToLower( GetFileExtension( file ) );
    if ( ext == "zip" || ext == "pk3" )
    {
        success = m_IrrFileSys->addZipFileArchive( file.c_str() );
        if ( success )
            g_kernel->LPrintf( ("--Successfully loaded: " + file).c_str() );
        else
            g_kernel->LPrintf( ("**Failed to load: " + file).c_str() );
    } else  g_kernel->LPrintf( ("**WARNING:: " + file + " is not of type PK3 or ZIP!" ).c_str() );
    
    return success;
}

/** Change the working Dir */
bool IrrVFSystem::ChangeWorkingDir( const std::string &dir )
{
    return m_IrrFileSys->changeWorkingDirectoryTo( dir.c_str() );
}

/** Get the working dir */
const std::string IrrVFSystem::GetWorkingDir()
{
    const irr::c8* str = m_IrrFileSys->getWorkingDirectory();
    std::string mstr = str;

    return mstr;
}

/** Get the file directory */
const std::string IrrVFSystem::GetFileDir( const std::string &file )
{
    irr::core::stringc s = file.c_str();
    irr::core::stringc str = m_IrrFileSys->getFileDir( s );
    std::string mstr = str.c_str();

    return mstr;
}

/** Test to see if file exists */
bool IrrVFSystem::FileExists( const std::string &f )
{
    return m_IrrFileSys->existFile( f.c_str() );
}

/** Create a file for writing */
MFile* IrrVFSystem::CreateVFile( const std::string &fileName )
{
    return NULL;
}

/** Open a file */
MFile* IrrVFSystem::OpenFile( const std::string &file )
{
    irr::io::IReadFile* irrFile = m_IrrFileSys->createAndOpenFile( file.c_str() );
    if ( irrFile )
    {
        int sizeOfFile = irrFile->getSize();

        char* buffer = new char[ sizeOfFile ];
        sizeOfFile = irrFile->read( buffer, sizeOfFile );

        MFile* mFile = new MFile;
        mFile->SetData( buffer, sizeOfFile );

        return mFile;
    } else return NULL;
}